
/**
 * Date:2019年6月3日上午9:22:46
 * Copyright (c) 2019, www.bwbroad.com All Rights Reserved.
 *
*/

package com.xue.im.server.handler;

import java.util.List;

import com.xue.im.common.IMUtil;
import com.xue.im.common.IMUtil.MessageType;
import com.xue.im.common.vo.Message;
import com.xue.im.common.vo.User;
import com.xue.im.server.core.IMServerContext;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

/**
 * Description: 登录响应处理 <br/>
 * Date: 2019年6月3日 上午9:22:46 <br/>
 * 
 * @author xuejianxin
 */
public class LoginAuthResponseHandler extends ChannelInboundHandlerAdapter {

	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
		Message message = (Message) msg;
		if (IMUtil.checkMessageType(message, MessageType.LOGIN_REQ)) {// 登录请求
			login(ctx, message);
		} else if (IMUtil.checkMessageType(message, MessageType.USERLIST_REQ)) {// 用户列表请求
			List<User> uList = IMServerContext.getAllUser();
			ctx.writeAndFlush(IMUtil.buildMessageObject(MessageType.USERLIST_RESP, uList));
		} else {
			ctx.fireChannelRead(msg);
		}
	}

	// 用户登录
	private void login(ChannelHandlerContext ctx, Message message) {
		boolean success = true;
		User returnUser = new User();
		returnUser.setStatus(IMUtil.USER_STATUS_OFFLINE);

		// 判断是否重复登录，如果重复登录则拒绝
		// if (!IMServerContext.hasNode(ctx)) {
		String userid = (String) message.getOption(IMUtil.USER_NAME);
		String password = (String) message.getOption(IMUtil.PASSWORD);
		User user = IMServerContext.getUser(userid);

		if (null == user) {// 用户不存在
			returnUser.setStatus(IMUtil.USER_STATUS_USER_NOT_EXIST);
			success = false;
		} else if (!user.getPassword().equals(password)) {// 密码不正确
			returnUser.setStatus(IMUtil.USER_STATUS_PASSWORD_NOT_CORRECT);
			success = false;
		}

		if (success) {
			IMServerContext.putNode(ctx);
			IMServerContext.saveUserCtx(userid, ctx);
			//保存服务器端用户状态
			user.setStatus(IMUtil.USER_STATUS_ONLINE);
			user.setIp(IMServerContext.getClientIp(ctx));// 设置用户登录ip
			
			// 设置登录成功
			returnUser.setStatus(user.getStatus());
			returnUser.setUsername(user.getUsername());
			returnUser.setSex(user.getSex());
			returnUser.setIp(user.getIp());
			
			IMServerContext.broadcastOnlineMessage(ctx, user);
		}
		// }
		// 构建登录响应消息
		Message loginResponse = IMUtil.buildMessageObject(MessageType.LOGIN_RESP, returnUser);
		// 写出响应消息
		ctx.writeAndFlush(loginResponse);
	}

	// 连接异常处理
	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
		ctx.fireExceptionCaught(cause);// 传播异常
		close(ctx);
	}

	// 连接激活
	// @Override
	// public void channelActive(ChannelHandlerContext ctx) throws Exception {}

	// 断掉连接
	@Override
	public void channelInactive(ChannelHandlerContext ctx) throws Exception {
		close(ctx);
	}

	// 关闭通道
	private void close(ChannelHandlerContext ctx) {
		IMServerContext.clearUserCtx(ctx);
		ctx.close();
	}

}
